#include "prj_task_detail_widget.h"
#include "ui_prj_task_detail_widget.h"
#include "global_manager.h"
#include "easylogging++.h"
#include "task_info_table.h"
#include <QStyledItemDelegate>

PrjTaskDetailWidget::PrjTaskDetailWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PrjTaskDetailWidget),
    m_sqlOperator(new SqliteManager)
{
    ui->setupUi(this);
    initUi();
    initEvents();
}

PrjTaskDetailWidget::~PrjTaskDetailWidget()
{
    delete ui;
}

void PrjTaskDetailWidget::setTaskUuid(QString uuid)
{
    if (m_taskUuid == uuid)
    {
        return ;
    }

    m_taskUuid = uuid;
    initTaskData(uuid);
}

void PrjTaskDetailWidget::setTaskInfo(QString taskName, QString taskDescription)
{
    ui->taskNameLineEdit->setText(taskName);
    ui->taskDescriptionTextEdit->setText(taskDescription);
}

void PrjTaskDetailWidget::initUi()
{
    setAttribute(Qt::WA_StyledBackground);
    ui->taskNameLineEdit->setReadOnly(true);
    ui->taskDescriptionTextEdit->setReadOnly(true);
    ui->editBtn->show();
    ui->cancelBtn->hide();
    ui->saveBtn->hide();

    QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
    ui->statusComboBox->setItemDelegate(itemDelegate);
}

void PrjTaskDetailWidget::initEvents()
{    
    connect(ui->editBtn, &QPushButton::clicked, this, [=]() {
        ui->editBtn->hide();
        ui->cancelBtn->show();
        ui->saveBtn->show();
        ui->taskNameLineEdit->setReadOnly(false);
        ui->taskDescriptionTextEdit->setReadOnly(false);
    });
    connect(ui->cancelBtn, &QPushButton::clicked, this, [=]() {
        ui->taskNameLineEdit->setReadOnly(true);
        ui->taskDescriptionTextEdit->setReadOnly(true);
        ui->editBtn->show();
        ui->cancelBtn->hide();
        ui->saveBtn->hide();
        initTaskData(m_taskUuid);
    });
    connect(ui->saveBtn, &QPushButton::clicked, this, &PrjTaskDetailWidget::onSaveBtnClicked);
    connect(ui->statusComboBox, &QComboBox::currentTextChanged, this, [=](){
        TaskInfoTable tit;
        tit.updateTaskStatus(m_taskUuid, ui->statusComboBox->currentText());
    });
}

void PrjTaskDetailWidget::initTaskData(QString uuid)
{
    QList<QString> fields({"task_name", "task_description", "task_status"});
    QList<QVariantList> data;
    QHash<QString, QVariant> whereConditions;
    whereConditions["uuid"] = uuid;
    bool ret = m_sqlOperator->selectData(TASK_INFO_TABLE_NAME, fields, whereConditions, data);

    if (!ret || data.empty())
    {
        return ;
    }

    ui->taskNameLineEdit->setText(data[0][0].toString());
    ui->taskDescriptionTextEdit->setText(data[0][1].toString());
    QString status = data[0][2].toString();
    if (status.isEmpty())
    {
        ui->statusComboBox->setCurrentIndex(0);
    }
    else
    {
        ui->statusComboBox->setCurrentText(status);
    }
}

void PrjTaskDetailWidget::onSaveBtnClicked()
{
    QHash<QString, QVariant> data;
    data["task_name"] = ui->taskNameLineEdit->text();
    data["task_description"] = ui->taskDescriptionTextEdit->toPlainText();
    QHash<QString, QVariant> whereConditions;
    whereConditions["uuid"] = m_taskUuid;
    bool ret = m_sqlOperator->updateData(TASK_INFO_TABLE_NAME, data, whereConditions);
    if (!ret)
    {
        LOG(ERROR) << "update data failed, error info: "
                   << m_sqlOperator->lastError().toStdString();
    }

    ui->taskNameLineEdit->setReadOnly(true);
    ui->taskDescriptionTextEdit->setReadOnly(true);
    ui->editBtn->show();
    ui->cancelBtn->hide();
    ui->saveBtn->hide();
}
